package com.alks.function.service.impl.stockdept.pcfactorystockquery;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcDrawReplace;
import com.alks.entity.data.entity.PcSlipTemp;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.LotSubMtlDto;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.OtMtlOutDto;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.ReplaceDto;
import com.alks.function.data.dto.stockdept.pcfactorystockwork.StyMtlDto;
import com.alks.function.data.request.stockdept.pcfactorystockwork.DrawActMtlRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.OMtlwaitAddRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.StyMtlRequest;
import com.alks.function.mapper.common.BackMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.*;
import com.alks.function.service.stockdept.pcfactorystockquery.IPcOtherMtlOutService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
public class PcOtherMtlOutServiceImpl implements IPcOtherMtlOutService {
    private final SD_SQ_PcSlipTempMapper tempMapper;
    private final SD_SQ_PcDrawReplaceMapper replaceMapper;
    private final BackMapper backMapper;
    private final SD_SQ_SdMaterialMapper materialMapper;
    private final SD_SQ_PcLotUsageMapper usageMapper;
    private final SD_SQ_SdBomPartNew2Mapper partNew2Mapper;
    private final SD_SQ_SdBomMtlMapper mtlMapper;
    private final SD_SQ_SdBomFitSubMapper subMapper;

    @Override
    public ResponseInfo getOtherMtlOutDtlQuery2() {
        /*获取数据*/
        String userName = UserIdThread.get().getUserName();
        List<OtMtlOutDto> dtos = tempMapper.getOtherMtlOutDtlQuery2(userName);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getOtherMtlOutSubQuery2(DrawActMtlRequest request) {
        /*获取出库类型*/
        List<String> list = backMapper.outSlipReasonQuery();
        /*获取数据*/
        request.setPageNum(request.getPageNum()==null||request.getPageNum()<=0?1:request.getPageNum());
        request.setPageSize(request.getPageSize()==null||request.getPageSize()<=0?10:request.getPageSize());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        LambdaQueryWrapper<PcDrawReplace> wrapper = new LambdaQueryWrapper<PcDrawReplace>()
                .eq(!request.getSlipNo().isEmpty(), PcDrawReplace::getSlipNo, request.getSlipNo())
                .eq(!request.getMtlName1().isEmpty(), PcDrawReplace::getMtlName, request.getMtlName1())
                .eq(!request.getMtlName2().isEmpty(), PcDrawReplace::getMtlName2, request.getMtlName2())
                .in(PcDrawReplace::getOutType, list)
                .orderByDesc(PcDrawReplace::getSlipNo);
        List<PcDrawReplace> replaces = replaceMapper.selectList(wrapper);
        Page<PcDrawReplace> page =(Page<PcDrawReplace>) replaces;
        /*提取数据*/
        List<ReplaceDto> dtos = BeanUtil.copyList(replaces, ReplaceDto.class);
        /*封装数据*/
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    @AutoPageAop
    public ResponseInfo getStyMtlQuery2(StyMtlRequest request) {
        /*获取数据*/
        List<StyMtlDto> dtos=partNew2Mapper.getStyMtlQuery2(request);
        Page<StyMtlDto> page=(Page<StyMtlDto>) dtos;
        /*封装数据*/
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        map.put("total",page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo otherMtlWaitStockAdd2(List<OMtlwaitAddRequest> request, Integer type) {
        SysUserTokenDTO tokenDTO = UserIdThread.get();
        String companyId = tokenDTO.getCompanyId();
        String userName = tokenDTO.getUserName();
        /*获取全部物料编码*/
        List<String> mtlNos = request.stream().map(OMtlwaitAddRequest::getMtlNo).collect(Collectors.toList());
        /*判断是否有重复数据*/
        LambdaQueryWrapper<PcSlipTemp> wrapper=new LambdaQueryWrapper<PcSlipTemp>()
                .in(PcSlipTemp::getMtlNo, mtlNos)
                .eq(PcSlipTemp::getSlipType,"O")
                .eq(PcSlipTemp::getCompanyId,companyId)
                .eq(PcSlipTemp::getSysUser,userName);
        List<PcSlipTemp> temps = tempMapper.selectList(wrapper);
        if (!(temps ==null||temps.isEmpty())){
            return ResponseInfo.error("存在重复物料，物料编码为\n"+temps.get(0).getMtlNo(), INPUT_ERROR.getCode());
        }
        /*分别获取数据*/
        switch (type){
            case 1:
                temps=materialMapper.getOtMtlWaitStk2(request);
                break;
            case 2:
                temps=usageMapper.getOtMtlWaitStk2(request);
                break;
            case 3:
                temps=partNew2Mapper.getOtMtlWaitStk2(request);
                break;
            case 4:
                temps=mtlMapper.getOtMtlWaitStk2(request);
                break;
            default:
                log.error("请求接口：{}\n错误原因：类型超出范围限制\n错误参数 type：{}"
                        ,this.getClass().getName(),type);
                return ResponseInfo.error("请求参数错误，请联系后台程序员", INPUT_ERROR.getCode());
        }
        /*插入创建人和时间*/
        for (PcSlipTemp temp : temps) {
            temp.setSysDate(LocalDateTime.now());
            temp.setSysUser(userName);
        }
        /*写入数据*/
        tempMapper.otherMtlWaitStockAdd2(temps);
        return ResponseInfo.ok("新增成功");
    }

    @Override
    public ResponseInfo otherLotSubMtlQuery2(String mtlNo) {
        /*获取数据*/
        List<LotSubMtlDto> dtos = subMapper.otherLotSubMtlQuery(mtlNo);
        return ResponseInfo.ok(dtos);
    }

}
